var express = require('express');
var router = express.Router();
const moment = require('moment');
//应聘list
router.get('/apply/list', function (req, res, next) {
    res.render('admin_tmpl/apply/list.html', {});
});

//应聘添加
router.get('/apply/add', async (req, res, next) => {
    res.render('admin_tmpl/apply/add.html', {});
});

//应聘查看
router.get('/apply/edit', async (req, res, next) => {
    var id = req.query.id;
    var applyData;
    var recruitmentId;
    var recruitmentData;
    var SQL = 'select * from t_apply where delete_flag = 0 and id="' + id + '"';
    await common.prototype.operation(SQL).then( async rest => {
        applyData = rest[0];
        recruitmentId = applyData.recruitment_id;
    })
    var recruitmentSQL = 'select * from t_recruitment where delete_flag = 0 and id="' + recruitmentId + '"';
    await common.prototype.operation(recruitmentSQL).then( async rest => {
        recruitmentData = rest[0];

    })
    res.render('admin_tmpl/apply/edit.html', {apply:applyData,recruitment:recruitmentData});
});

/*
 * 应聘列表
 * @return
 *
 */
router.post('/apply/getList', async (req, res, next) => {
    var condition = '';
    var SQL = '';
    var conditionSQL = '';
    var allData = '';
    var begin = 0;
    var data = req.body;
    var pageSize = data.pageSize;
    var pageNumber = data.pageNumber;
    var likeName = req.body.search_like_name;
    var likeEducation = req.body.search_like_education;
    var likeMail = req.body.search_like_mail;
    var likePhone = req.body.search_like_phone;
    if (likeName) {
        condition = condition + 'and position("' + likeName + '" IN name)'
    }
    if (likeEducation) {
        condition = condition + 'and position("' + likeEducation + '" IN education)'
    }
    if (likeMail) {
        condition = condition + 'and position("' + likeMail + '" IN mail)'
    }
    if (likePhone) {
        condition = condition + 'and position("' + likePhone + '" IN phone)'
    }
    SQL = 'select * from t_apply where delete_flag = 0 ' + condition + ''
    await common.prototype.operation(SQL).then(async rest => {
        if (pageSize) {
            begin = (pageNumber - 1) * pageSize;
            condition = condition + 'order by id desc limit ' + begin + ',' + pageSize + ''
        }
        allData = rest;
        conditionSQL = 'select * from t_apply where delete_flag = 0 ' + condition + ''
        await common.prototype.operation(conditionSQL).then(async result => {
            res.send({
                code: 0, msg: '操作成功',
                count: allData.length, data: result
            })
        })
    }, (error) => {
        console.log(error)
        res.send({
            code: 404, msg: '操作失败',
            data: error
        })
    }).catch((err) => {
        console.log(err)
    })
});

/*
 * 删除应聘
 * @return
 *
 */
router.post('/apply/delete', async (req, res, next) => {
    var data = req.body;
    if (data.id) {
        var date = moment().format('YYYY-MM-DD HH:mm:ss');
        var SQL = 'update t_apply set delete_flag=1, modify_date="' + date + '" where id="' + data.id + '"'
        await common.prototype.operation(SQL).then(rest => {
            res.send({
                type: 'success', content: '删除成功'
            })
        }, (error) => {
            console.log(error)
            res.send({
                type: 'error', content: '删除失败'
            })
        }).catch((err) => {
            console.log(err)
        })
    } else {
        res.send({
            type: 'apiexception', content: '接口异常'
        })
    }
});


module.exports = router;